package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.AGoodsCategory;
import com.ruoyi.system.service.IAGoodsCategoryService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AGoods;
import com.ruoyi.system.service.IAGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品Controller
 * 
 * @author ruoyi
 * @date 2024-12-11
 */
@RestController
@RequestMapping("/dianshang/goods")
public class AGoodsController extends BaseController
{
    @Autowired
    private IAGoodsService aGoodsService;

    @Autowired
    private IAGoodsCategoryService aGoodsCategoryService;

    /**
     * 查询商品列表
     */
//    @PreAuthorize("@ss.hasPermi('dianshang:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(AGoods aGoods)
    {
        startPage();
        List<AGoods> list = aGoodsService.selectAGoodsList(aGoods);
        for(int i=0;i<list.size();i++){
            AGoodsCategory aGoodsCategory = aGoodsCategoryService.selectAGoodsCategoryById(list.get(i).getGc());
            list.get(i).setLeixing(aGoodsCategory.getName());
        }
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @PreAuthorize("@ss.hasPermi('dianshang:goods:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AGoods aGoods)
    {
        List<AGoods> list = aGoodsService.selectAGoodsList(aGoods);
        ExcelUtil<AGoods> util = new ExcelUtil<AGoods>(AGoods.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('dianshang:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AGoods aGoods = aGoodsService.selectAGoodsById(id);
        AGoodsCategory aGoodsCategory = aGoodsCategoryService.selectAGoodsCategoryById(aGoods.getGc());
        aGoods.setLeixing(aGoodsCategory.getName());
        return success(aGoods);
    }

    /**
     * 新增商品
     */
    @PreAuthorize("@ss.hasPermi('dianshang:goods:add')")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AGoods aGoods)
    {
        return toAjax(aGoodsService.insertAGoods(aGoods));
    }

    /**
     * 修改商品
     */
    @PreAuthorize("@ss.hasPermi('dianshang:goods:edit')")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AGoods aGoods)
    {
        return toAjax(aGoodsService.updateAGoods(aGoods));
    }

    /**
     * 删除商品
     */
    @PreAuthorize("@ss.hasPermi('dianshang:goods:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(aGoodsService.deleteAGoodsByIds(ids));
    }
}
